package com.gebogebo.android.justads;

import static java.lang.String.format;

import java.util.Date;

import android.content.SharedPreferences;
import android.location.Location;
import android.util.Log;

public class UserPreferences {
    private static final String lg = "prefActivity";
    
    public static String USER_GENDER_KEY = "gender";
    public static String MARITAL_KEY = "marital_status";
    public static String AGE_KEY = "age";
    
    public enum Gender {MALE, FEMALE};
    public enum MaritalStatus {SINGLE, MARRIED, DIVORCED};
    
    private Date birthdate;
    private Location location;
    private Gender userGender;
    private MaritalStatus maritalStatus;
    private int age;
    
    public UserPreferences(SharedPreferences prefs) {
        String gender = prefs.getString(USER_GENDER_KEY, null);
        if(gender != null) {
            try {
                userGender = Gender.valueOf(gender.toUpperCase());
            } catch(Throwable t) {
                Log.e(lg, format("exception thrown while converting configured gender %s", maritalStatus));
                userGender = null;
            }
            Log.i(lg, format("set gender to %s", userGender));
        }

        String maritalStatusString = prefs.getString(MARITAL_KEY, null);
        if(maritalStatusString != null) {
            try {
                maritalStatus = MaritalStatus.valueOf(maritalStatusString.toUpperCase());
            } catch(Throwable t) {
                Log.e(lg, format("exception thrown while converting configured marital status %s", maritalStatus));
                maritalStatus = null;
            }
            Log.i(lg, format("set marital status to %s", maritalStatus));
        }
        
        String ageStr = prefs.getString(AGE_KEY, "-1");
        if(ageStr != null) {
            try {
                age = Integer.parseInt(ageStr);
            } catch(Throwable t) {
                Log.e(lg, "non-parsable int set as age");
                age = -1;
            }
        }
    }
    
    public Gender getUserGender() {
        return userGender;
    }
    
    public MaritalStatus getUserMaritalStatus() {
        return maritalStatus;
    }
    
    public Location getLocation() {
        return location;
    }

    public Date getBirthdate() {
        return birthdate;
    }
    
    public int getAge() {
        return age;
    }
}
